package app.common.utils;

import app.common.utils.cjt.ao.OtherQueryQueryWithDetailAo;
import app.common.utils.cjt.ao.SettingShopQueryShopAo;
import app.common.utils.cjt.ao.WmsStockPdQueryStockPdInDetailAo;
import app.common.utils.cjt.ao.WmsStockPdQueryStockPdOutDetailAo;
import app.common.utils.cjt.ao.WmsStockStockSyncByPdAo;
import app.common.utils.cjt.ao.WmsStockinOtherQueryWithDetailAo;
import app.common.utils.cjt.ao.WmsStockinProcessQueryWithDetailAo;
import app.common.utils.cjt.ao.WmsStockinTransferQueryWithDetailAo;
import app.common.utils.cjt.ao.WmsStockoutProcessQueryWithDetailAo;
import app.common.utils.cjt.ao.WmsStockoutTransferQueryWithDetailAo;
import app.common.utils.cjt.vo.OtherOutboundOrderQueryVo;
import app.common.utils.cjt.vo.SettingShopQueryShopVo;
import app.common.utils.cjt.vo.WdtResp;
import app.common.utils.cjt.vo.WmsStockPdQueryStockPdInDetailVo;
import app.common.utils.cjt.vo.WmsStockPdQueryStockPdOutDetailVo;
import app.common.utils.cjt.vo.WmsStockinOtherQueryWithDetailVo;
import app.common.utils.cjt.vo.WmsStockinProcessQueryWithDetailVo;
import app.common.utils.cjt.vo.WmsStockinTransferQueryWithDetailVo;
import app.common.utils.cjt.vo.WmsStockoutProcessQueryWithDetailVo;
import app.common.utils.cjt.vo.WmsStockoutTransferQueryWithDetailVo;
import app.common.utils.wdt.sdk.Client;
import app.common.utils.wdt.sdk.Pager;
import app.common.utils.wdt.sdk.WdtErpException;
import app.common.utils.wdt.sdk.impl.DefaultClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

/**
 * 旺电通接口
 * no read
 * @author feizns
 * @since 2023/04/04
 */
public abstract class WdtUtils {

    /**
     * sid
     */
    private static final String sid = "weikang3";

    /**
     * app Key
     */
    private static final String appKey = "weikang3-ot";

    /**
     * app Secret
     */
    private static final String appSecret = "60912045fdb3e0f2f920d880bffeff51:44830636146232969aa35d00cf4f828f";

    /**
     * 客户端
     */
    public static final Client client = DefaultClient.get(sid, appKey, appSecret);

    /**
     * 其他入库单查询
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void wmsStockinOtherQueryWithDetail(WmsStockinOtherQueryWithDetailAo ao, Consumer<WmsStockinOtherQueryWithDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockin.Other.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockinOtherQueryWithDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * 其他入库单查询
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<WmsStockinOtherQueryWithDetailVo.Order> wmsStockinOtherQueryWithDetail(WmsStockinOtherQueryWithDetailAo ao) throws IOException, WdtErpException {
        List<WmsStockinOtherQueryWithDetailVo.Order> orders = new ArrayList<>();
        wmsStockinOtherQueryWithDetail(ao, orders::add);
        return orders;
    }

    /**
     * 调拨入库单请求参数
     * @param ao 请求参数
     * @return {@link List}<{@link WmsStockinTransferQueryWithDetailVo.Order}>
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<WmsStockinTransferQueryWithDetailVo.Order> wmsStockinTransferQueryWithDetailAo(WmsStockinTransferQueryWithDetailAo ao) throws IOException, WdtErpException {
        List<WmsStockinTransferQueryWithDetailVo.Order> results = new ArrayList<>();
        wmsStockinTransferQueryWithDetailAo(ao, results::add);
        return results;
    }

    /**
     * 调拨入库单请求参数
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void wmsStockinTransferQueryWithDetailAo(WmsStockinTransferQueryWithDetailAo ao, Consumer<WmsStockinTransferQueryWithDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockin.Transfer.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockinTransferQueryWithDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * 生产入库单查询
     * 当前位置： API文档 > 库存类
     * wms.stockin.Process.queryWithDetail（生产入库单查询）
     */
    public static void wmsStockinProcessQueryWithDetail(WmsStockinProcessQueryWithDetailAo ao, Consumer<WmsStockinProcessQueryWithDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockin.Process.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockinProcessQueryWithDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * 盘点入库单请求参数
     * @param ao 请求参数
     * @return {@link List}<{@link WmsStockPdQueryStockPdInDetailVo.Order}>
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<WmsStockPdQueryStockPdInDetailVo.Order> wmsStockPdQueryStockPdInDetail(WmsStockPdQueryStockPdInDetailAo ao) throws IOException, WdtErpException {
        List<WmsStockPdQueryStockPdInDetailVo.Order> results = new ArrayList<>();
        wmsStockPdQueryStockPdInDetail(ao, results::add);
        return results;
    }

    /**
     * 盘点入库单请求参数
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void wmsStockPdQueryStockPdInDetail(WmsStockPdQueryStockPdInDetailAo ao, Consumer<WmsStockPdQueryStockPdInDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.StockPd.queryStockPdInDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockPdQueryStockPdInDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<WdtResp<WmsStockPdQueryStockPdInDetailVo>>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    //******************************出库单************************************
    //******************************出库单************************************
    //******************************出库单************************************

    /**
     * 其他出库单查询
     * @param ao 请求参数
     * @return {@link List}<{@link OtherOutboundOrderQueryVo.Order}>
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<OtherOutboundOrderQueryVo.Order> otherQueryQueryWithDetail(OtherQueryQueryWithDetailAo ao) throws IOException, WdtErpException {
        List<OtherOutboundOrderQueryVo.Order> results = new ArrayList<>();
        otherQueryQueryWithDetail(ao, results::add);
        return results;
    }

    /**
     * 其他出库单查询
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void otherQueryQueryWithDetail(OtherQueryQueryWithDetailAo ao, Consumer<OtherOutboundOrderQueryVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockout.OtherQuery.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<OtherOutboundOrderQueryVo> wdtResp = JSON.parseObject(resp, new TypeReference<WdtResp<OtherOutboundOrderQueryVo>>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * wms.stockout.Transfer.queryWithDetail（调拨出库单查询）
     * @param ao 请求参数
     * @return {@link List}<{@link WmsStockoutTransferQueryWithDetailVo.Order}>
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<WmsStockoutTransferQueryWithDetailVo.Order> wmsStockoutTransferQueryWithDetail(WmsStockoutTransferQueryWithDetailAo ao) throws IOException, WdtErpException {
        List<WmsStockoutTransferQueryWithDetailVo.Order> results = new ArrayList<>();
        wmsStockoutTransferQueryWithDetail(ao, results::add);
        return results;
    }

    /**
     * wms.stockout.Transfer.queryWithDetail（调拨出库单查询）
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void wmsStockoutTransferQueryWithDetail(WmsStockoutTransferQueryWithDetailAo ao, Consumer<WmsStockoutTransferQueryWithDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockout.Transfer.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockoutTransferQueryWithDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<WdtResp<WmsStockoutTransferQueryWithDetailVo>>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * 生产出库查询
     * 当前位置： API文档 > 库存类
     * wms.stockout.Process.queryWithDetail（生产出库查询）
     */
    public static void wmsStockoutProcessQueryWithDetail(WmsStockoutProcessQueryWithDetailAo ao, Consumer<WmsStockoutProcessQueryWithDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.stockout.Process.queryWithDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockoutProcessQueryWithDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<WdtResp<WmsStockoutProcessQueryWithDetailVo>>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * wms.StockPd.queryStockPdOutDetail（盘点出库单查询 ）
     * @param ao 请求参数
     * @return {@link List}<{@link WmsStockoutTransferQueryWithDetailVo.Order}>
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static List<WmsStockPdQueryStockPdOutDetailVo.Order> wmsStockPdQueryStockPdOutDetail(WmsStockPdQueryStockPdOutDetailAo ao) throws IOException, WdtErpException {
        List<WmsStockPdQueryStockPdOutDetailVo.Order> results = new ArrayList<>();
        wmsStockPdQueryStockPdOutDetail(ao, results::add);
        return results;
    }

    /**
     * wms.StockPd.queryStockPdOutDetail（盘点出库单查询 ）
     * @param ao 请求参数
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void wmsStockPdQueryStockPdOutDetail(WmsStockPdQueryStockPdOutDetailAo ao, Consumer<WmsStockPdQueryStockPdOutDetailVo.Order> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("wms.StockPd.queryStockPdOutDetail", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<WmsStockPdQueryStockPdOutDetailVo> wdtResp = JSON.parseObject(resp, new TypeReference<WdtResp<WmsStockPdQueryStockPdOutDetailVo>>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getOrder().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     * 获取设置查询商店购物
     * 店铺查询
     * 当前位置： API文档 > 基础类
     * setting.Shop.queryShop（店铺查询）
     * @param ao 请求参数
     * @param consumer 消费者
     * @throws IOException     ioexception
     * @throws WdtErpException wdt erp异常
     */
    public static void fetchSettingShopQueryShop(SettingShopQueryShopAo ao, Consumer<SettingShopQueryShopVo.Details> consumer) throws IOException, WdtErpException {
        long total;
        int curt = 0;
        long maxPageSize = 500;
        do {
            String resp = client.execute("setting.Shop.queryShop", JSON.toJSONString(Collections.singletonList(ao)), new Pager((int) maxPageSize, curt, true));
            WdtResp<SettingShopQueryShopVo> wdtResp = JSON.parseObject(resp, new TypeReference<>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
            total = wdtResp.getData().getTotal_count();
            curt++;
            wdtResp.getData().getDetails().forEach(consumer);
        } while ( curt * maxPageSize < total );
    }

    /**
     *
     */
    public static void wmsStockPdStockSyncByPd(WmsStockStockSyncByPdAo.PdOrder order, WmsStockStockSyncByPdAo.SpecList... list) {
        try {
            String execute = client.execute("wms.StockPd.stockSyncByPd", JSON.toJSONString(Arrays.asList(order, Arrays.asList(list))), null);
            WdtResp<String> wdtResp = JSON.parseObject(execute, new TypeReference<>() {});
            if ( !wdtResp.isSuccess() )
                throw new RuntimeException("" + wdtResp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
